var UserContribEDJX = {
    btn_adduser: document.getElementById('select_contrib_addbtn'),
    btn_searchuser: document.getElementById('search_contrib_addbtn'),
    input_searchuser: document.getElementById('search_contrib_input'),
    select_contrib_search_invalid: document.getElementById('select_contrib_search_invalid'),
    select_contrib_search_noresult: document.getElementById('select_contrib_search_nouser'),
    search_waiter: document.getElementById('search_contrib_wait'),
    select_contrib: document.getElementById('select_contrib'),
    select_contrib_container_add_user: document.getElementById('select_contrib_container_add_user'),
    select_contrib_search_error: document.getElementById('select_contrib_search_error'),
    select_contrib_listuser: document.getElementById('select_contrib_listuser'),
    contributors_select_container: document.getElementById('contributors_select_container'),
    contributors_contrib_wait: document.getElementById('contributors_contrib_wait'),
    select_contrib_listuser_btn: document.getElementById('select_contrib_listuser_btn'),
    search_waiter: document.getElementById('search_contrib_wait'),
    select_contrib: document.getElementById('select_contrib'),
    select_contrib_container_add_user: document.getElementById('select_contrib_container_add_user'),
    select_contrib_delete_error: document.getElementById('select_contrib_delete_error'),
    
    init: function() {
	try {
	    UserContribEDJX.listAll();
	} catch(e) {
	    setTimeout('UserContribEDJX.init()', 700);
	}
    },
    
    listAll: function() {
	UserContribEDJX.showWaiter();
	UserContribEDJX.input_searchuser.disabled = true;
	UserContribEDJX.btn_searchuser.disabled = true;
	frame = document.createElement('iframe');
	frame.setAttribute('src', '/edjx/contrib/list/' + tool_name);
	frame.setAttribute('name', 'ajax_callback_x_frame');
	frame.setAttribute('style', 'display: none;');
	document.getElementsByTagName('body')[0].appendChild(frame);
	frame.onload = UserContribEDJX.reset_search();
    },
    
    update: function(names, ids) {
	var selObj = UserContribEDJX.select_contrib_listuser;
	selObj.options.length = 0;
	for(x in names) {
	    selObj.options[selObj.options.length] = new Option(names[x], ids[x]);
	}
	UserContribEDJX.showSelect();
    },
    
    listUserChangeEvent: function() {
	var val = UserContribEDJX.select_contrib_listuser.value
	if(val != '') {
	    UserContribEDJX.select_contrib_listuser_btn.disabled = false;
	} else {
	    UserContribEDJX.select_contrib_listuser_btn.disabled = true;
	}
    },
    
    delEvent: function() {
	UserContribEDJX.showWaiter_search();
	if(UserContribEDJX.select_contrib_listuser.options.value != '') {
	    frame = document.createElement('iframe');
	    frame.setAttribute('src', '/edjx/contrib/del/' + tool_name + '/' + UserContribEDJX.select_contrib_listuser.value);
	    frame.setAttribute('name', 'ajax_callback_x_frame');
	    frame.setAttribute('style', 'display: none;');
	    document.getElementsByTagName('body')[0].appendChild(frame);
	} else {
	    UserContribEDJX.select_contrib_listuser_btn.disabled = true;
	}
    },
    
    showWaiter: function() {
	UserContribEDJX.showWaiter_search();
	UserContribEDJX.contributors_select_container.style.display = "none";
	UserContribEDJX.contributors_select_container.style.visibility = "hidden";
	UserContribEDJX.contributors_contrib_wait.style.display = "inline";
	UserContribEDJX.contributors_contrib_wait.style.visibility = "visible";
    },
    
    showSelect: function() {
	UserContribEDJX.reset_search();
	UserContribEDJX.contributors_select_container.style.display = "inline";
	UserContribEDJX.contributors_select_container.style.visibility = "visible";
	UserContribEDJX.contributors_contrib_wait.style.display = "none";
	UserContribEDJX.contributors_contrib_wait.style.visibility = "hidden";
	UserContribEDJX.searchChangeEvent();
    },
    
    showWaiter_search: function() {
	UserContribEDJX.search_waiter.style.display = "inline";
	UserContribEDJX.search_waiter.style.visibility = "visible";
	UserContribEDJX.searchUnderMinLength(false); // Hide the warnings
	UserContribEDJX.select_contrib_container_add_user.style.display = "none";
	UserContribEDJX.select_contrib_container_add_user.style.visibility = "hidden";
    },
    
    showSelect_search: function() {
	UserContribEDJX.search_waiter.style.display = "none";
	UserContribEDJX.search_waiter.style.visibility = "hidden";
	UserContribEDJX.select_contrib_container_add_user.style.display = "inline";
	UserContribEDJX.select_contrib_container_add_user.style.visibility = "visible";
    },
    
    hideWaiter_search: function() {
	UserContribEDJX.search_waiter.style.display = "none";
	UserContribEDJX.search_waiter.style.visibility = "hidden";
    },
    
    reset_search: function() {
	UserContribEDJX.input_searchuser.disabled = false;
	UserContribEDJX.input_searchuser.value = "";
	UserContribEDJX.btn_searchuser.disabled = true;
	UserContribEDJX.hideWaiter_search();
    },
    
    searchChangeEvent: function() {
	var val = UserContribEDJX.input_searchuser.value;
	if(val != '') {
	    UserContribEDJX.btn_searchuser.disabled = false;
	} else {
	    UserContribEDJX.btn_searchuser.disabled = true;
	}
    },
    
    addEvent: function() {
	UserContribEDJX.showWaiter();
	if(UserContribEDJX.select_contrib.options.value != '') {
	    frame = document.createElement('iframe');
	    frame.setAttribute('src', '/edjx/contrib/add/' + tool_name + '/' + UserContribEDJX.select_contrib.value);
	    frame.setAttribute('name', 'ajax_callback_x_frame');
	    frame.setAttribute('style', 'display: none;');
	    document.getElementsByTagName('body')[0].appendChild(frame);
	} else {
	    UserContribEDJX.btn_adduser.disabled = true;
	}
    },
    
    searchEvent: function() {
	UserContribEDJX.showWaiter_search();
	UserContribEDJX.input_searchuser.disabled = true;
	UserContribEDJX.btn_searchuser.disabled = true;
	frame = document.createElement('iframe');
	frame.setAttribute('src', '/edjx/contrib/search/' + tool_name + '/' + UserContribEDJX.input_searchuser.value);
	frame.setAttribute('name', 'ajax_callback_x_frame');
	frame.setAttribute('style', 'display: none;');
	document.getElementsByTagName('body')[0].appendChild(frame);
    },
    

    searchSelect_change: function() {
	if(UserContribEDJX.select_contrib.options.value != '') {
	    UserContribEDJX.btn_adduser.disabled = false;
	} else {
	    UserContribEDJX.btn_adduser.disabled = true;
	}
    },
    /*
    updateSearch: function(ids, names, boolNone) {
	UserContribEDJX.searchNoneFound((boolNone || ids.length == 0) ? true : false);
	if(boolNone == true) {
	    UserContribEDJX.select_contrib.options.length = 0;
	    UserContribEDJX.reset_search();
    */
    updateSearch: function(ids, names, boolNone) {
	if(boolNone == true) {
	    UserContribEDJX.select_contrib.options.length = 0;
	    UserContribEDJX.searchUnderMinLength(true);
	} else {
	    UserContribEDJX.select_contrib.options.length = 0;
	    UserContribEDJX.searchUnderMinLength(false);
	    for(x in ids) {
		var the_id = ids[x];
		var the_name = names[x];
		UserContribEDJX.select_contrib.options[UserContribEDJX.select_contrib.options.length] = new Option(the_name, the_id);
	    }
	    UserContribEDJX.showSelect_search();
	    UserContribEDJX.reset_search();
	}
    },
    
    searchUnderMinLength: function(boolIn) {
	if(boolIn == true) {
	    UserContribEDJX.select_contrib_search_invalid.style.display = "inline";
	    UserContribEDJX.select_contrib_search_invalid.style.visibility = "visible";
	    UserContribEDJX.hideWaiter_search();
	    UserContribEDJX.reset_search();
	} else {
	    UserContribEDJX.select_contrib_search_invalid.style.display = "none";
	    UserContribEDJX.select_contrib_search_invalid.style.visibility = "hidden";
	}
    },
    
    searchNoneFound: function(boolIn) {
	if(boolIn == true) {
	    UserContribEDJX.select_contrib_search_noresult.style.display = "inline";
	    UserContribEDJX.select_contrib_search_noresult.style.visibility = "visible";
	} else {
	    UserContribEDJX.select_contrib_search_noresult.style.display = "none";
	    UserContribEDJX.select_contrib_search_noresult.style.visibility = "hidden";
	}
    },
    
    /**
     * Callback for adder
     */
    addError: function(erMsg) {
	if(erMsg == false) {
	    UserContribEDJX.select_contrib_search_error.style.display = "none";
	    UserContribEDJX.select_contrib_search_error.style.visibility = "hidden";
	} else {
	    select_contrib_search_error.innerHTML = erMsg;
	    UserContribEDJX.select_contrib_search_error.style.display = "inline";
	    UserContribEDJX.select_contrib_search_error.style.visibility = "visible";
	    UserContribEDJX.showSelect_search();
	}
	UserContribEDJX.listAll();
    },
    
    /*
     * Deletion Callback
     */
    delError: function(erMsg) {
	if(erMsg == false) {
	    UserContribEDJX.select_contrib_delete_error.innerHTML = "";
	    UserContribEDJX.select_contrib_delete_error.style.display = "none";
	    UserContribEDJX.select_contrib_delete_error.style.visibility = "hidden";
	    UserContribEDJX.listAll();
	} else {
	    UserContribEDJX.select_contrib_delete_error.style.display = "inline";
	    UserContribEDJX.select_contrib_delete_error.style.visibility = "visible";
	    UserContribEDJX.select_contrib_delete_error.innerHTML = erMsg + '<br />';
	    UserContribEDJX.listAll();
	}
    }
};
setTimeout('UserContribEDJX.init()', 1750);